package com.mall507.fivepeaks.activity.find;

import android.support.annotation.NonNull;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.ViewGroup;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.hzh.frame.comn.annotation.ViewInject;
import com.hzh.frame.comn.callback.HttpCallBack;
import com.hzh.frame.core.HttpFrame.BaseHttp;
import com.hzh.frame.ui.activity.BaseUI;
import com.mall507.fivepeaks.R;
import com.mall507.fivepeaks.activity.main.MainConvenientLifeChuShouRFM;
import com.mall507.fivepeaks.activity.main.MainConvenientLifeChuZuRFM;
import com.mall507.fivepeaks.activity.main.MainConvenientLifeErShouMaiMaiRFM;
import com.mall507.fivepeaks.activity.main.MainConvenientLifeQiuGouRFM;
import com.mall507.fivepeaks.activity.main.MainConvenientLifeQiuZhiRFM;
import com.mall507.fivepeaks.activity.main.MainConvenientLifeQiuZuRFM;
import com.mall507.fivepeaks.activity.main.MainConvenientLifeZhaoPinRFM;
import com.mall507.fivepeaks.activity.main.MainConvenientLifeZhuanRangRFM;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 便民生活
 */
@Route(path = "/find/FindMaiFangUI")
public class FindMaiFangUI extends BaseUI {

    @ViewInject(R.id.tablayout)
    TabLayout mTabLayout;
    @ViewInject(R.id.viewPager)
    ViewPager mViewPager;

//    private ArrayList<String> stateName = new ArrayList<>(); //标题
    private ArrayList<Integer> stateValue = new ArrayList<>();
    String[] stateName=new String[]{"出售","转让"};
    MyPagerAdapter myPagerAdapter;

    @Override
    protected void onCreateBase() {
        setContentView(R.layout.ui_find_convenient_life);
        getTitleView().setContent("便民生活");
        getComment();
    }

    private void initViewPager() {
        // 创建一个集合,装填Fragment
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(MainConvenientLifeChuShouRFM.newInstance(stateValue.get(4)));
        fragments.add(MainConvenientLifeZhuanRangRFM.newInstance(stateValue.get(6)));
        // 创建ViewPager适配器
        myPagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
        myPagerAdapter.setFragments(fragments);
        // 给ViewPager设置适配器
        mViewPager.setAdapter(myPagerAdapter);
        // TabLayout 指示器 (记得自己手动创建4个Fragment,注意是 app包下的Fragment 还是 V4包下的 Fragment)
        for (int i = 0; i < stateValue.size(); i++) {
            mTabLayout.addTab(mTabLayout.newTab());
        }
        // 使用 TabLayout 和 ViewPager 相关联
        mTabLayout.setupWithViewPager(mViewPager);
        // TabLayout指示器添加文本
        for (int i = 0; i < stateName.length; i++) {
            //            mTabLayout.getTabAt(i).setText(stateName.get(i));
            mTabLayout.getTabAt(i).setText(stateName[i]);
        }
        //tab的字体选择器,默认黑色,选择时红色
        mTabLayout.setTabTextColors(ContextCompat.getColor(this, R.color.white), ContextCompat.getColor(this, R.color.white));
        //tab的下划线颜色,默认是粉红色
        mTabLayout.setSelectedTabIndicatorColor(ContextCompat.getColor(this, R.color.white));
    }

    public class MyPagerAdapter extends FragmentPagerAdapter {

        //当前Fragment
        private List<Fragment> mFragmentList;

        @Override
        //每次Fragment切换都会调用到FragmentPagerAdapter.setPrimaryItem 方法
        public void setPrimaryItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            super.setPrimaryItem(container, position, object);
        }

        public void setFragments(ArrayList<Fragment> fragments) {
            mFragmentList = fragments;
        }

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {

            Fragment fragment = mFragmentList.get(position);

            return fragment;
        }

        @Override
        public int getCount() {

            return mFragmentList.size();
        }
    }

    private void getComment() {
        BaseHttp.getInstance().query("release/getNav", new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject response) {
                JSONArray data = response.optJSONArray("data");
                if (data != null && data.length() > 0) {
                    for (int i = 0; i < data.length(); i++) {
                        JSONObject object = data.optJSONObject(i);
//                        stateName.add(object.optString("catname"));
                        stateValue.add(Integer.valueOf(object.optString("catid")));
                    }
                }
                initViewPager(); // 初始化ViewPager
                mViewPager.setOffscreenPageLimit(stateValue.size() + 1);
            }
        });
    }
}
